package com.region.loadbalancer.monitor.event;

import com.region.loadbalancer.group.GroupServer;
import com.region.loadbalancer.group.Server;
import com.region.loadbalancer.group.ServerState;
import com.region.loadbalancer.monitor.ServerMonitor;

/**
 * Detection Anomaly Event
 *
 * @author liujieyu
 * @date 2023/5/25 23:34
 * @desciption
 */
public class AnomalyEvent extends AbstractEvent {

    private Server anomalyServer;

    public AnomalyEvent(GroupServer groupServer, Server anomalyServer) {
        super(groupServer);
        this.anomalyServer = anomalyServer;
    }

    @Override
    public EventState state() {
        return EventState.SCHEDULE;
    }

    @Override
    public void event(GroupServer groupServer) {
        boolean status = groupServer.getDetectionConnection().connect(anomalyServer);
        // If the service is connected
        if (status) {
            anomalyServer.setStatus(ServerState.UP);
            groupServer.getManager().executeConnectListener(anomalyServer);
        } else {
            ServerMonitor.getInstance().submitAsyncEvent(this);
        }
    }
}
